package com.bizark.rpc.api.share_serv.warehouse.region;

import com.bizark.rpc.api.bean.base.BaseListResponse;
import com.bizark.rpc.api.bean.base.BaseQueryRequest;
import com.bizark.rpc.api.bean.base.BaseResponse;
import com.bizark.rpc.api.bean.region.Region;
import com.bizark.rpc.api.exception.RpcException;
import com.bizark.rpc.common.tool.BeanUtil;
import com.bizark.rpc.core.service.region.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by tsj on 2017/3/15.
 */
@Service
public class RegionProviderImpl implements RegionProvider {

    @Autowired
    private RegionService regionService;

    /**
     * 通过id查询region
     * @param id
     * @return
     */
    @Override
    public BaseResponse<Region> getRegionById(String id) {
        Region region =  regionService.getRegionById(id);
        BaseResponse<Region> response = new BaseResponse<>();
        response.setData(region);
        return response;
    }

    @Override
    public BaseListResponse<Region> getRegions() {
        return null;
    }

    /**
     * 新增region
     * @param region
     * @return
     * @throws RpcException
     */
    @Override
    public BaseResponse<Region> addRegion(Region region)  throws RpcException{
        try {
//            转换入参
//            Region region  = (Region)(BeanUtil.mapToObject(params,Region.class));

            regionService.addRegion(region);
            BaseResponse<Region> response = new BaseResponse<>();
            //查出插入的数据
            Region rep =  regionService.getRegionById(region.getId());

            response.setData(rep);
            return response;
        } catch(RpcException rpcException)
        {
            throw rpcException;
        }catch (Exception e) {
            throw new RpcException("500","内部错误");
        }

    }


    @Override
    public BaseResponse<Region> updateRegion(Region region) throws RpcException {
        regionService.updateRegion(region);
        BaseResponse<Region> response = new BaseResponse<>();
        response.setData(region);
        return response;
    }

    /**
     * 删除单个对象
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public int deleteRegionById(String id) throws Exception {
        int i = regionService.deleteRegion(id);
        return i;
    }


    /**
     * 分页查询
     * @param request
     * @return
     */
    @Override
    public BaseListResponse<Region> getPage(BaseQueryRequest request){

        Region region =new Region();
        regionService.getRegionsPage(region);
        return null;
    }

}
